/*
 * NPFNewItemDialog.java
 *
 * Created on March 18, 2007, 11:25 PM
 */

package newpodfeed;

import javax.swing.JComponent;
import java.util.*;
import java.io.*;

/**
 *
 * @author  newpodfeed
 */
public class NPFNewItemDialog extends javax.swing.JDialog {
    
    Vector <JComponent>vector;
    NPFTextDialog dialog;
    NPFDate date;
    NPFFileFilter filter;
    NPFFeedFileFilter feedFilter;
    NPFConfirmDialog confirm;
    javax.swing.JFileChooser chooser;
    FileInputStream fis;
    FileOutputStream fos;
    ObjectInputStream ois;
    ObjectOutputStream oos;
    
    /** Creates new form NPFNewItemDialog */
    public NPFNewItemDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        vector = new Vector();
        vector.add(jLabel7);
        vector.add(authorField);
        vector.add(jLabel8);
        vector.add(keywordsField);
        vector.add(jLabel9);
        vector.add(durationField);
        dialog = new NPFTextDialog(NPFMainFrame.frame, true);
        date = new NPFDate();
        filter = new NPFFileFilter();
        feedFilter = new NPFFeedFileFilter();
        confirm = new NPFConfirmDialog(NPFMainFrame.frame, true);
        chooser = new javax.swing.JFileChooser();
        chooser.setFileFilter(filter);
        dialog.clear();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        titleField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        descriptionField = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        urlField = new javax.swing.JTextField();
        filesizeField = new javax.swing.JTextField();
        iTunesCheckBox = new javax.swing.JCheckBox();
        jLabel7 = new javax.swing.JLabel();
        authorField = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        keywordsField = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        durationField = new javax.swing.JTextField();
        generateButton = new javax.swing.JButton();
        closeButton = new javax.swing.JButton();
        loadTemplateButton = new javax.swing.JButton();
        saveTemplateButton = new javax.swing.JButton();
        labelNotes = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        areaNotes = new javax.swing.JTextArea();
        jLabel3 = new javax.swing.JLabel();
        feedFileField = new javax.swing.JTextField();
        feedFileButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("New Release");

        jLabel1.setText("Title");

        titleField.setColumns(20);
        titleField.setToolTipText("Title of the show");

        jLabel2.setText("Description");

        descriptionField.setColumns(20);
        descriptionField.setToolTipText("Description of the show");

        jLabel4.setText("Enclosure");

        jLabel5.setText("URL");

        jLabel6.setText("File Size");

        urlField.setColumns(20);
        urlField.setToolTipText("Link to the MP3 for the new show");

        filesizeField.setColumns(20);
        filesizeField.setToolTipText("File size of the show's MP3");

        iTunesCheckBox.setText("iTunes");
        iTunesCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        iTunesCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                iTunesCheckBoxActionPerformed(evt);
            }
        });

        jLabel7.setText("Author");
        jLabel7.setEnabled(false);

        authorField.setColumns(20);
        authorField.setToolTipText("Creator of the show");
        authorField.setEnabled(false);

        jLabel8.setText("Keywords");
        jLabel8.setEnabled(false);

        keywordsField.setColumns(20);
        keywordsField.setToolTipText("Keywords and tags for the show");
        keywordsField.setEnabled(false);

        jLabel9.setText("Duration");
        jLabel9.setEnabled(false);

        durationField.setColumns(20);
        durationField.setToolTipText("Duration of the show in MM:SS or HH:MM:SS format");
        durationField.setEnabled(false);

        generateButton.setText("Update Feed");
        generateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                generateButtonActionPerformed(evt);
            }
        });

        closeButton.setText("Close");
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });

        loadTemplateButton.setText("Load Template");
        loadTemplateButton.setToolTipText("Fill out certain fields with information from a template file");
        loadTemplateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loadTemplateButtonActionPerformed(evt);
            }
        });

        saveTemplateButton.setText("Save Template");
        saveTemplateButton.setToolTipText("Save the information from certain fields as a template for reuse later");
        saveTemplateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveTemplateButtonActionPerformed(evt);
            }
        });

        labelNotes.setText("Notes");

        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        areaNotes.setColumns(25);
        areaNotes.setLineWrap(true);
        areaNotes.setRows(5);
        areaNotes.setText("Add notes about the podcast here.\nThese notes aren't saved in the podcast RSS feed, they're just saved in the template file for your own information.");
        areaNotes.setWrapStyleWord(true);
        jScrollPane1.setViewportView(areaNotes);

        jLabel3.setText("Feed File");

        feedFileField.setColumns(20);
        feedFileField.setToolTipText("The podcast RSS XML feed file");

        feedFileButton.setText("...");
        feedFileButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                feedFileButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(filesizeField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(urlField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addComponent(iTunesCheckBox)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(authorField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(keywordsField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(durationField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(generateButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(loadTemplateButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(saveTemplateButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(closeButton))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(labelNotes)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(descriptionField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(titleField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(feedFileField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(feedFileButton)))
                .addContainerGap(45, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(titleField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(feedFileField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(feedFileButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(descriptionField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(urlField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(filesizeField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(iTunesCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(authorField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(keywordsField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(durationField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelNotes)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(generateButton)
                    .addComponent(loadTemplateButton)
                    .addComponent(saveTemplateButton)
                    .addComponent(closeButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void saveTemplateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveTemplateButtonActionPerformed
        //Vector vector = new Vector();
        String[] fields = new String[7];
        File file;
        int result;
        chooser.setFileFilter(filter);
        result = chooser.showSaveDialog(this);
        if (result == javax.swing.JFileChooser.CANCEL_OPTION) {
            return;
        }
        file = chooser.getSelectedFile();
        if (!file.getPath().endsWith(".npf")) {
            String s = file.getPath();
            s += ".npf";
            file = new File(s);
        }
        if (file.exists()) {
            confirm.setVisible(true);
            int confirmStatus = confirm.getReturnStatus();
            if (confirmStatus == confirm.RET_CANCEL) {
                return;
            }
        }
        System.out.println(file.getPath());
        fields[0] = titleField.getText();
        /* The category field in an item appears to be redundant
        fields[1] = categoryField.getText(); */
        /* Keeping the field as blank maintains compatibility
         * with older .npf versions.
         */
        fields[1] = "blank";
        fields[2] = authorField.getText();
        fields[3] = keywordsField.getText();
        fields[4] = "" + iTunesCheckBox.isSelected();
        fields[5] = areaNotes.getText();
        fields[6] = feedFileField.getText();
        try {
            fos = new FileOutputStream(file);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(fields);
            oos.flush();
            fos.flush();
            oos.close();
            fos.close();
        }
        catch(Exception e) {
            System.out.println("Error writing template:  " + e);
        }
    }//GEN-LAST:event_saveTemplateButtonActionPerformed

    private void loadTemplateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadTemplateButtonActionPerformed
        String[] fields = new String[7];
        File file;
        int fileVersion;
        int result;
        chooser.setFileFilter(filter);
        result = chooser.showOpenDialog(this);
        if (result == javax.swing.JFileChooser.CANCEL_OPTION) {
            return;
        }
        file = chooser.getSelectedFile();
        try {
            fis = new FileInputStream(file);
            ois = new ObjectInputStream(fis);
            fields = (String[]) ois.readObject();
            ois.close();
            fis.close();
        }
        catch(Exception e) {
            System.out.println("Error reading template:  " + e);
        }
        titleField.setText(fields[0]);
        //categoryField.setText(fields[1]);
        authorField.setText(fields[2]);
        keywordsField.setText(fields[3]);
        if (Boolean.parseBoolean(fields[4])) {
            /* Make sure the check box is selected properly */
            if (!iTunesCheckBox.isSelected()) {
                iTunesCheckBox.doClick();
            }
        }
        else {
            if (iTunesCheckBox.isSelected()) {
                iTunesCheckBox.doClick();
            }
        }
        areaNotes.setText(fields[5]);
        
        try {
            feedFileField.setText(fields[6]);
        }
        catch(Exception e) {
            NPFDialog feedback = new NPFDialog(new javax.swing.JFrame(), true);
            
            feedback.clear();
            feedback.setText("Please save your template file again to update it to the latest template file version.");
            feedback.setVisible(true);
        }
    }//GEN-LAST:event_loadTemplateButtonActionPerformed

    private void iTunesCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_iTunesCheckBoxActionPerformed
        JComponent component;
        boolean enabled;
        if (iTunesCheckBox.isSelected()) {
            enabled = true;
        }
        else {
            enabled = false;
        }
        for(int l = 0; l < vector.size(); l++) {
            component = vector.get(l);
            component.setEnabled(enabled);
        }
    }//GEN-LAST:event_iTunesCheckBoxActionPerformed

    private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed
        setVisible(false);
    }//GEN-LAST:event_closeButtonActionPerformed

    private void generateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_generateButtonActionPerformed
        File file = new File(feedFileField.getText());
        NPFDialog feedback = new NPFDialog(new javax.swing.JFrame(), true);
        
        if (file.exists() != true) {
            feedback.clear();
            feedback.setText("The specified feed file does not exist.");
            feedback.setVisible(true);
            return;
        }
        
        if (file.canWrite() != true) {
            feedback.clear();
            feedback.setText("The specified feed file cannot be written to.");
            feedback.setVisible(true);
            return;
        }
        
        try {
            //String path = file.getPath().substring(0, file.getPath().lastIndexOf(File.separator));
            File tempFile = File.createTempFile("npf", "npftemp", file.getParentFile());
            BufferedReader feedReader = new BufferedReader(new FileReader(file));
            BufferedWriter feedWriter = new BufferedWriter(new FileWriter(tempFile));
            String input = "";
            boolean updated = false;
            
            input = feedReader.readLine();
            feedWriter.write(input + "\n");
            input = feedReader.readLine();
            feedWriter.write(input + "\n");
            input = feedReader.readLine();
            
            if (input.equals("<!-- npf -->") != true) {
                feedback.clear();
                feedback.append("This feed is not currently being managed by NewPodFeed.\n\n");
                feedback.append("To convert this feed to NewPodFeed, go to the main NewPodFeed window and click on Menu > Convert Feed.\n\n");
                feedback.append("This will not alter how your feed works in any other program.  It will simply allow NewPodFeed to begin managing your feed.");
                feedback.setVisible(true);
                tempFile.delete();
                return;
            }
            
            feedWriter.write(input + "\n");
            
            while(true) {
                input = feedReader.readLine();
                
                if (input == null) {
                    break;
                }
                
                if (input.startsWith("<lastBuildDate>")) {
                    feedWriter.write("<lastBuildDate>" + date + "</lastBuildDate>\n");
                    continue;
                }
                
                if (input.equals("<!-- item insert point -->")) {
                    feedWriter.write(input + "\n");
                    feedWriter.write("\n");
                    addItemText(feedWriter);
                    updated = true;
                    continue;
                }
                
                feedWriter.write(input);
                feedWriter.write("\n");
            }
            
            feedWriter.flush();
            feedWriter.close();
            feedReader.close();
            
            file.delete();
            tempFile.renameTo(file);
            
            if (updated == true) {
                feedback.clear();
                feedback.setText("Updating of feed " + file.getName() + " complete.");
                feedback.setVisible(true);
            }
            else {
                feedback.clear();
                feedback.setText("No updates were made to the feed.");
                feedback.setVisible(true);
            }
        }
        catch(Exception e) {
            feedback.clear();
            feedback.append("There is an error with the feed reader.");
            feedback.append("\n\n*** Don't panic ***\n\n");
            feedback.append("The error log reads as follows:\n\n");
            feedback.append("" + e);
            feedback.append("\n\nIf you really want to, you can go to newpodfeed.googlecode.com and report this error.\n\n");
            feedback.append("Have a nice day, Citizen.");
            feedback.setVisible(true);
            return;
        }
        
        /*dialog.clear();
        dialog.println("<item>");
        dialog.println("<title>" + titleField.getText() + "</title>");
        if (iTunesCheckBox.isSelected()) {
            String description = descriptionField.getText();
            dialog.println("<itunes:author>" + authorField.getText() + "</itunes:author>");
            if (description.length() > 255) {
                description = description.substring(0, 255);
            }
            dialog.println("<itunes:subtitle>" + description + "</itunes:subtitle>");
            dialog.println("<itunes:summary>" + descriptionField.getText() + "</itunes:summary>");
            dialog.println("<itunes:keywords>" + keywordsField.getText() + "</itunes:keywords>");
            dialog.println("<itunes:duration>" + durationField.getText() + "</itunes:duration>");
        }
        dialog.println("<description>" + descriptionField.getText() + "</description>");
        //dialog.println("<category>" + categoryField.getText() + "</category>");
        dialog.println("<pubDate>" + date + "</pubDate>");
        dialog.println("<link>" + urlField.getText() + "</link>");
        dialog.println("<enclosure url=\"" + urlField.getText() + "\" length=\"" + filesizeField.getText() + "\" type=\"audio/mpeg\"/>");
        dialog.println("<guid>" + urlField.getText() + "</guid>");
        dialog.println("</item>");
        dialog.setVisible(true);*/
    }//GEN-LAST:event_generateButtonActionPerformed

    private void feedFileButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_feedFileButtonActionPerformed
        File file;
        int result;
        NPFDialog feedback = new NPFDialog(new javax.swing.JFrame(), true);
        
        /* Make sure the appropriate file filter is set */
        chooser.setFileFilter(feedFilter);
        
        result = chooser.showOpenDialog(this);
        
        if (result == javax.swing.JFileChooser.CANCEL_OPTION) {
            return;
        }
        
        file = chooser.getSelectedFile();
        
        if (file.exists() != true) {
            feedback.clear();
            feedback.setText("The selected file does not exist.");
            feedback.setVisible(true);
            return;
        }
        
        if (file.canWrite() != true) {
            feedback.clear();
            feedback.setText("The selected file cannot be written to.");
            feedback.setVisible(true);
            return;
        }
        
        feedFileField.setText(file.getPath());
    }//GEN-LAST:event_feedFileButtonActionPerformed
    
    private void addItemText(BufferedWriter feedWriter) throws Exception {
        feedWriter.write("<item>\n");
        feedWriter.write("<title>" + titleField.getText() + "</title>\n");
        if (iTunesCheckBox.isSelected()) {
            String description = descriptionField.getText();
            feedWriter.write("<itunes:author>" + authorField.getText() + "</itunes:author>\n");
            if (description.length() > 255) {
                description = description.substring(0, 255);
            }
            feedWriter.write("<itunes:subtitle>" + description + "</itunes:subtitle>\n");
            feedWriter.write("<itunes:summary>" + descriptionField.getText() + "</itunes:summary>\n");
            /* Keywords have been deprecated by iTunes */
            //feedWriter.write("<itunes:keywords>" + keywordsField.getText() + "</itunes:keywords>\n");
            feedWriter.write("<itunes:duration>" + durationField.getText() + "</itunes:duration>\n");
        }
        feedWriter.write("<description>" + descriptionField.getText() + "</description>\n");
        //dialog.println("<category>" + categoryField.getText() + "</category>");
        feedWriter.write("<pubDate>" + date + "</pubDate>\n");
        feedWriter.write("<link>" + urlField.getText() + "</link>\n");
        feedWriter.write("<enclosure url=\"" + urlField.getText() + "\" length=\"" + filesizeField.getText() + "\" type=\"audio/mpeg\"/>\n");
        feedWriter.write("<guid>" + urlField.getText() + "</guid>\n");
        feedWriter.write("</item>\n");
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NPFNewItemDialog(new javax.swing.JFrame(), true).setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea areaNotes;
    private javax.swing.JTextField authorField;
    private javax.swing.JButton closeButton;
    private javax.swing.JTextField descriptionField;
    private javax.swing.JTextField durationField;
    private javax.swing.JButton feedFileButton;
    private javax.swing.JTextField feedFileField;
    private javax.swing.JTextField filesizeField;
    private javax.swing.JButton generateButton;
    private javax.swing.JCheckBox iTunesCheckBox;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField keywordsField;
    private javax.swing.JLabel labelNotes;
    private javax.swing.JButton loadTemplateButton;
    private javax.swing.JButton saveTemplateButton;
    private javax.swing.JTextField titleField;
    private javax.swing.JTextField urlField;
    // End of variables declaration//GEN-END:variables
    
}
